package com.mdz.myapplication.Controller;

import com.mdz.myapplication.Entity.person;
import de.felixroske.jfxsupport.FXMLController;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.util.Callback;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author lake957
 */
@FXMLController
public class personController implements Initializable {

	@FXML
	private TableView<person> table;
	@FXML
	private TableColumn<person,String> name;
	@FXML
	private Button checkbtn;
	@FXML
	private CheckBox selectAll;
	@FXML
	private TableColumn<person, CheckBox> checklist;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		ObservableList<person> data = FXCollections.observableArrayList(
				new person("1"),
				new person("2"),
				new person("3")
		);
		name.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().name) );
		checklist.setCellValueFactory(cellData ->cellData.getValue().cb.getCheckBox());
		table.setItems(data);
		//初始化所有checkbox复选框
		checklist.setCellValueFactory(cellData ->cellData.getValue().cb.getCheckBox());
		//初始化复选框-监听-所有checkbox复选框都勾选则全选框勾选，所有checkbox复选框不勾选则全选框不勾选
		Callback<TableColumn<person, CheckBox>, TableCell<person, CheckBox>> cellFactory1 =
				new Callback<TableColumn<person, CheckBox>, TableCell<person, CheckBox>>() {
					@Override
					public TableCell call(TableColumn p) {
						TableCell cell = new TableCell<person, CheckBox>() {
							@Override
							public void updateItem(CheckBox item, boolean empty) {
								super.updateItem(item, empty);
								setGraphic(item);
								if (item!= null) {
									item.selectedProperty().addListener(new ChangeListener<Boolean>() {
										@Override
										public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
											boolean booSelectAll = true;
											ObservableList<person> items2 = table.getItems();
											for (person cxCadreInfo2:items2){
												if (!cxCadreInfo2.cb.isSelected()) {
													booSelectAll = false;
													break;
												}
											}
											selectAll.setSelected(booSelectAll);
										}
									});
								}
							}
						};
						return cell;
					}
				};
		checklist.setCellFactory(cellFactory1);

	}
	@FXML
	public void selectBox(){
		ObservableList<person> items = table.getItems();
		if (selectAll.isSelected()){
			for (person cxCadreInfo:items){
				cxCadreInfo.cb.getCheckBox().getValue().setSelected(false);
			}
		}else {
			for (person cxCadreInfo:items){
				cxCadreInfo.cb.getCheckBox().getValue().setSelected(true);
			}
		}
	}

	@FXML
	public void check()
	{
		ObservableList<person> list=table.getItems();
		for(person o:list)
		{
			if(o.cb.isSelected())
			{
				System.out.println(o);
				System.out.println(o.name);
			}
		}
	}
}
